package com.gmail.yesguxun.tumblr.ui;

import com.gmail.yesguxun.tumblr.Constant;
import com.gmail.yesguxun.tumblr.TrAccount;
import com.gmail.yesguxun.tumblr.presenter.TrSettingPresenter;
import com.gmail.yesguxun.util.LogFactory;
import com.gmail.yesguxun.util.LogX;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class SettingActivity extends Activity {
	
	private LogX mLog = LogFactory.getLogObject();
	private Context mActivityContext = null;
	private Button mButtonAuthorize = null;
	private EditText mEditEMail = null; 
	private EditText mEditPassword = null;
	private TrSettingPresenter  mPresenter = null;
	private Spinner mSpinnerSizeSelect = null;
	private Spinner mSpinnerViewModeSelect = null;
	
	private ProgressDialog mProgressDialog = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
	    
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.setting);
		
		mLog.debug("SettingActivity onCreate");
		TrAccount account = TrAccount.getTrAccount();
        if(!account.isAccountInfoValid())
        {
            account.setContext(getApplicationContext());
            account.load();     
        }
		
		mButtonAuthorize = (Button) findViewById(R.id.buttonAuthorize);
		mEditEMail = (EditText) findViewById(R.id.editEmail);
		mEditPassword = (EditText) findViewById(R.id.editPassword);
		mActivityContext = this;
		
		mSpinnerSizeSelect = (Spinner) findViewById(R.id.spinnerSizeSelect);
		ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.size_selection, android.R.layout.simple_spinner_item);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		mSpinnerSizeSelect.setAdapter(adapter);
		mSpinnerSizeSelect.setSelection(adapter.getPosition(Constant.ACCOUNT_PREFS_PIC_SIZE_VALUE_DEFAULT));
		
		mSpinnerViewModeSelect = (Spinner) findViewById(R.id.spinnerModeSelect);
        ArrayAdapter<CharSequence> adapter1 = ArrayAdapter.createFromResource(this, R.array.view_mode_selection, android.R.layout.simple_spinner_item);
        adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        mSpinnerViewModeSelect.setAdapter(adapter1);
        mSpinnerViewModeSelect.setSelection(adapter1.getPosition(Constant.ACCOUNT_PREFS_VIEW_MODE_VALUE_DEFAULT));
		
		if(mPresenter == null){
			mPresenter = new TrSettingPresenter(this);
			mPresenter.bind();
		}		
	}
	
	@Override
	protected void onStart() {
	      
		super.onStart();	
		
		mLog.debug("SettingActivity onStart");
	}
	
	public View getButtonAuthorize()
	{
		return this.mButtonAuthorize;	
	}
	
	public TextView getEditTextEMail()
	{
		return this.mEditEMail;
	}
	
	public TextView getEditTextPassword()
	{
		return this.mEditPassword;
	}
	
	public Spinner getSelectPictueSizeSpinner()
	{
		return this.mSpinnerSizeSelect;
	}
	
	public Spinner getSelectViewModeSpinner()
    {
        return this.mSpinnerViewModeSelect;
    }
	
	public Context getContext()
	{
		return this.mActivityContext;
	}
	
	public void onInvalidInput()
	{
		Toast toast = Toast.makeText(this.getApplicationContext(), R.string.info_need_account_info, Toast.LENGTH_SHORT);
    	toast.show();
	}
	
	public void onStartAuthorization()
	{
		this.mProgressDialog = ProgressDialog.show(this.getContext(), "Wait..." , this.getResources().getString(R.string.info_in_authorization_process), true, false);
	}
	
	public void onEndAuhorization(boolean resultAuthorization)
	{
		if(this.mProgressDialog != null){
		    this.mProgressDialog.dismiss();
		    this.mProgressDialog = null;
		}
		
		if(resultAuthorization == false)
		{
		  Toast toast = Toast.makeText(this.getApplicationContext(), R.string.info_authorization_failed, Toast.LENGTH_SHORT);
    	  toast.show();
		}
	}

}
